from selenium import webdriver
from selenium.webdriver.common.by import By
from page_locators.login_page_locator import LoginPageLocator as LL
from comm.base_page import BasePage

class LoginPage(BasePage):

    def __init__(self, driver):
        # self.driver = webdriver.Chrome()
        # self.driver = driver
        super().__init__(driver)
        self.ll = LL()

    #登录页面 静态属性(元素的定位)
    # username_loc = (By.XPATH, '//*[@id="username"]')
    # password_loc = (By.XPATH, '//*[@id="password"]')
    # ver_code_loc = (By.XPATH, '//*[@id="verify_code"]')
    # loginbtn_loc = (By.XPATH, '//*[@id="loginform"]/div/div[6]/a')
    # errmsg_loc = (By.CSS_SELECTOR, '[class="layui-layer-content layui-layer-padding"]')
    # logout_loc = (By.CSS_SELECTOR, '[title="退出"]')

    def login(self,username,password,ver_code='1111'):
        """
        登录操作
        :param username: 用例中用户名数据
        :param password: 用例中密码数据
        :param ver_code: 用例中验证码数据(默认为1111)
        :return:
        """
        # self.driver.find_element(*self.ll.username_loc).send_keys(username)
        # self.driver.find_element(*self.ll.password_loc).send_keys(password)
        # self.driver.find_element(*self.ll.ver_code_loc).send_keys(ver_code)
        # self.driver.find_element(*self.ll.loginbtn_loc).click()
        self.input_context(self.ll.username_loc, username, "登录模块_登录页面_用户名输入")
        self.input_context(self.ll.password_loc, password, "登录模块_登录页面_密码输入")
        self.input_context(self.ll.ver_code_loc, ver_code, "登录模块_登录页面_验证码输入")
        self.click_element(self.ll.loginbtn_loc, "登录模块_登录页面_登录按钮点击")


    # 获取异常数据的错误提示信息
    def get_err_msg_from_login(self):
        # text = self.driver.find_element(*self.ll.errmsg_loc).text
        # return text
        text = self.get_element_text(self.ll.errmsg_loc, "登录模块_登录页面_错误信息获取")
        return text

    # 判断安全退出是否存在
    def logout_isexist(self):
        if self.driver.find_element(*self.ll.logout_loc):
            return True
        else:
            return False
